const app = getApp();
import Http from '../../../utils/http.js';
const http = new Http();
import { touchFromTheLeftEdge } from '../../../utils/behavior';
Page({
    behaviors: [touchFromTheLeftEdge],
    /**
     * 页面的初始数据
     */
    data: {
        navbtn: '', //navbtn  01--操作记录进入-显示顶部导航
        type: '',
        reqObj: {
            '01': 'getTransportationDetails',
            '02': 'getFoodDetails',
            '03': 'getAccommodationDetails',
            '04': 'getTripDetails',
        },
        city: '',
        country: '',
        lat: '',
        lng: '',
        createuid: '',
        realnapicurl: '',
        realname: '',
        createtime: '',
        sceneImageArr: [],
        address: '',
        content: '',
        title: '',
        // 留言相关
        remarkId: '',
        commentList: [],
        commentcount: Number,
        toView: '', //跳转留言id
        commentTop: 101,
        tocommentid: '', //被回复的评论id,回复时传
        placeHoldComment: '欢迎留言讨论', //评论/回复内容
        touid: '',
        toreplyid: '',
        privateflg: '00',
        // 默认不点赞
        praiseflg: '00',
        praisecnt: 0,
        // 编辑相关
        show: false,
        actions: [
            // {
            //     name: '编辑',
            //     color: '#333333',
            //     id: 2,
            // },
            {
                name: '删除',
                color: '#FF455E',
                id: 1,
            },
        ],
        id: '',
        // 当前创建人id
        activeCreateUid: '',
        review: '',
        reviewType: '',
        commentType: '', //评论标识
        praiseType: '', //点赞标识
        typeLike: '', //评论内容点在标识
    },
    // 查看他人主页
    jumpOthersPage(e) {
        wx.navigateTo({
            url: `/functionPage/pages/othersHomePage/othersHomePage?uid=${e.currentTarget.dataset.uid}`,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.id && options.type) {
            this.setData({
                id: options.id,
                type: options.type,
                review: options.review,
                navbtn: options.navbtn, //navbtn  01--操作记录进入
            });
            let reviewType;
            let commentType;
            let praiseType;
            let typeLike;
            if (options.type === '01') {
                wx.setNavigationBarTitle({
                    title: '车食住行：租车',
                });
                reviewType = '11';
                commentType = '19';
                praiseType = '19';
                typeLike = '23';
            } else if (options.type === '02') {
                wx.setNavigationBarTitle({
                    title: '车食住行：美食/三餐',
                });
                reviewType = '14';
                commentType = '20';
                praiseType = '20';
                typeLike = '24';
            } else if (options.type === '03') {
                wx.setNavigationBarTitle({
                    title: '车食住行：住宿',
                });
                reviewType = '12';
                commentType = '21';
                praiseType = '21';
                typeLike = '25';
            } else if (options.type === '04') {
                wx.setNavigationBarTitle({
                    title: '车食住行：出行',
                });
                reviewType = '13';
                commentType = '22';
                praiseType = '22';
                typeLike = '26';
            }
            this.setData({
                reviewType,
                commentType,
                praiseType,
                typeLike,
            });
            this.getCSZXDetail(options.id, this.data.reqObj[options.type]);
        }
    },
    onReady() {
        this.footComment = this.selectComponent('#foot-comment');
    },
    // 查询车食住行详情
    getCSZXDetail(id, reqname) {
        http.getRequest(
            `cjMap/${reqname}?authId=${app.globalData.authId}&id=${id}`
        ).then((res) => {
            this.setData({
                city: res.data.city,
                country: res.data.country,
                lat: res.data.lat,
                lng: res.data.lng,
                activeCreateUid: res.data.createuid,
                createuid: res.data.createuid,
                realnapicurl: res.data.realnapicurl,
                realname: res.data.realname,
                createtime: res.data.createtime,
                address: res.data.address,
                content: res.data.excontent,
                title: res.data.title,
                sceneImageArr: res.data.picurl
                    ? JSON.parse(res.data.picurl)
                    : [],
                praiseflg: res.data.praiseflg,
                praisecnt: res.data.praisecnt,
                commentList: res.data.commentList,
                commentcount: res.data.commentcount,
            });
        });
    },

    // 删除车食住行
    deleteCSZX() {
        let data = {
            authId: app.globalData.authId,
            id: this.data.id,
            type: this.data.type,
            lat: app.globalData.locationLat,
            lng: app.globalData.locationLng,
        };
        http.getRequest('cjMap/deleteTFAT', data).then((res) => {
            if (res.status == 0) {
                let pages = getCurrentPages(); //当前页面栈

                let prevPage = pages[pages.length - 2];
                if (prevPage && typeof prevPage.refresh == 'function') {
                    prevPage.refresh();
                }

                let prevPage2 = pages[pages.length - 3];
                if (prevPage2 && typeof prevPage2.refresh == 'function') {
                    prevPage2.refresh();
                }

                let prevPage3 = pages[pages.length - 4];
                if (prevPage3 && typeof prevPage3.refresh == 'function') {
                    prevPage3.refresh();
                }

                wx.showToast({
                    title: '删除成功',
                    icon: 'success',
                    duration: 1500,
                });
                setTimeout(function () {
                    wx.navigateBack({
                        delta: 1,
                    });
                }, 2000);
            }
        });
    },

    // 导航
    navigate() {
        let _that = this;
        wx.openLocation({
            latitude: _that.data.lat,
            longitude: _that.data.lng,
            name: '详细地址',
            address: _that.data.address,
        });
    },

    // 预览图片
    previewImage(e) {
        let index = e.currentTarget.dataset.index;
        wx.previewImage({
            current: this.data.sceneImageArr[index],
            urls: this.data.sceneImageArr,
        });
    },

    //评论
    jumpOrRemark(e) {
        if (this.data.commentTop > 100) {
            this.setData({
                toView: 'comment',
                commentTop: 0,
            });
        }
        if (e.detail.setplaceHold == '00') {
            this.footComment.articalRemark();
            this.onResetInputContent();
        }
    },

    // 重置输入框内容
    onResetInputContent() {
        this.setData({
            placeHoldComment: '欢迎留言讨论',
            tocommentid: '',
            touid: this.data.createuid,
            privateflg: '00',
        });
    },

    scrollTopFun(e) {},

    remarkInfo(e) {
        let _that = this;
        _that.footComment.remarkFocus('01');
        let name = e.detail.battleTag;
        _that.setData({
            tocommentid: e.detail.contentid || e.detail.tocommentid, //被回复的评论id,回复时传
            placeHoldComment: '@' + name, //评论/回复内容
            touid: e.detail.touid,
            toreplyid: e.detail.toreplyid,
            privateflg: e.detail.privateflg,
        });
    },
    remarkEditChange(e) {
        this.setData({
            _scrollY: !e.detail.remarkEdit, //被回复的评论id,回复时传
        });
    },
    // 编辑功能
    edit() {
        this.setData({
            show: true,
        });
    },

    // 取消编辑
    onCancel() {
        this.setData({
            show: false,
        });
    },

    // 选中后
    onSelect(e) {
        let _that = this;
        if (e.detail.id === 1) {
            wx.showModal({
                title: '提示',
                content: '确认要删除该项吗？',
                success: (res) => {
                    if (res.confirm) {
                        _that.deleteCSZX();
                    }
                },
            });
        } else if (e.detail.id === 2) {
            wx.navigateTo({
                url: `../../../releasePage/pages/addFood/addFood?type=02&id=${_that.data.id}&cszx=${_that.data.type}`,
            });
        } else if (e.detail.id === 'remark-delete') {
            wx.showModal({
                title: '提示',
                content: '确认要删除该项吗？',
                success: function (res) {
                    if (res.confirm) {
                        _that.onCancel();
                        //评论删除请求
                        http.getRequest(
                            `cjMap/deleteComment?authId=${app.globalData.authId}&id=${_that.data.remarkId}`
                        ).then((res) => {
                            if (res.status == 0) {
                                wx.showToast({
                                    title: '删除成功',
                                    icon: 'none',
                                    duration: 1500,
                                });
                                _that.refreshRemark();
                            }
                        });
                    } else {
                        _that.onCancel();
                        console.log('点击取消回调');
                    }
                },
            });
        }
        this.onCancel();
    },
    //删除评论
    deleteRemark(e) {
        this.setData({
            show: true,
            actions: [
                {
                    name: '删除',
                    id: 'remark-delete',
                    color: '#FF455E',
                },
            ],
            remarkId: e.detail.id,
        });
    },
    refresh() {
        this.getCSZXDetail(this.data.id, this.data.type);
    },

    refreshRemark() {
        http.getRequest(
            `cjMap/${this.data.reqObj[this.data.type]}?authId=${
                app.globalData.authId
            }&id=${this.data.id}`
        ).then((res) => {
            this.setData({
                praiseflg: res.data.praiseflg,
                praisecnt: res.data.praisecnt,
                _scrollY: true,
                commentList: res.data.commentList,
                commentcount: res.data.commentcount,
                tocommentid: '', //被回复的评论id,回复时传
                placeHoldComment: '欢迎留言讨论', //评论/回复内容
            });
        });
    },
});
